package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.utils.MiMaUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class MONIMiMaAttr implements AdapterAttr {


    public static void main(String[] args) {
        String body = HttpRequest.get("https://api.mimaapp.cn/v1/game/category").execute().body();
        System.out.println(body);
    }


    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        List<GamesRes> gamesResList = new ArrayList<>();
        String games = HttpRequest.get("https://api.mimaapp.cn/v1/game/category").execute().body();
        String data = JSONObject.parseObject(games).getString("data");
        JSONArray jsonArray = JSONArray.parseArray(data);
        for(Object o : jsonArray) {
            String son = JSONObject.parseObject(o.toString()).getString("son");
            JSONArray jsonArray1 = JSONArray.parseArray(son);
            for (Object p : jsonArray1) {
                JSONObject jsonObject = JSONObject.parseObject(p.toString());
                GamesRes gamesRes = new GamesRes();
                String id = jsonObject.getString("goods_cate_id");
                String cate_name = jsonObject.getString("cate_name");
                gamesRes.setId(id);
                gamesRes.setGameName(cate_name);
                gamesResList.add(gamesRes);
            }
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String url = "https://api.mimaapp.cn/v1/game/partition?game_id="+gameId;
        Map<String, String> params = new HashMap<>();
        params.put("game_id",gameId);
        params.put("remove_general","1");
        String login = MiMaUtil.login(salePartner.getSaleKey(), salePartner.getOther());
        String data1 = JSONObject.parseObject(login).getString("data");
        String authorization = JSONObject.parseObject(data1).getString("authorization");
        String response = HttpRequest.get(url).header("Authorization", authorization).execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        String string = JSONObject.parseObject(jsonObject.getString("data")).getString("partitions");
        List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            String partition_id = datum.getString("partition_id");
            String title = datum.getString("title");
            InterServerRes interServer = new InterServerRes();
            interServer.setId(partition_id);
            interServer.setName(title);
            interServer.setUuid(UUIDGenerator.generateUuid6());
            interServerRes.add(interServer);
            List<InterServerRes> interServerRes1 = new ArrayList<>();
            String servers = datum.getString("son");
            if(StringUtils.isNotEmpty(servers)) {
                List<JSONObject> list = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject jsonObject1 : list) {
                    InterServerRes interServer1 = new InterServerRes();
                    interServer1.setId(jsonObject1.getString("partition_id"));
                    interServer1.setName(jsonObject1.getString("title"));
                    interServer1.setUuid(UUIDGenerator.generateUuid6());
                    interServerRes1.add(interServer1);
                    interServer.setInterServerRes(interServerRes1);
                }
            }
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String login = MiMaUtil.login(salePartner.getSaleKey(), salePartner.getOther());
        String data1 = JSONObject.parseObject(login).getString("data");
        String authorization = JSONObject.parseObject(data1).getString("authorization");
        Integer sub_cate_id = 0;
        Integer type_id = 0;
        if(gameId.equals("359")) {
            sub_cate_id = 290;
            type_id = 1203;
        }else if(gameId.equals("372")) {
            sub_cate_id = 291;
            type_id = 1237;
        }else if(gameId.equals("361")) {
            sub_cate_id = 292;
            type_id = 1216;
        }else if(gameId.equals("691")) {
            sub_cate_id = 781;
            type_id = 1715;
        }else if(gameId.equals("364")) {
            sub_cate_id = 293;
            type_id = 1232;
        }else if(gameId.equals("968")) {
            sub_cate_id = 774;
            type_id = 2329;
        }else if(gameId.equals("934")) {
            sub_cate_id = 728;
            type_id = 7242;
        }else if(gameId.equals("1568")) {
            sub_cate_id = 1667;
            type_id = 3493;
        }

        String url = "https://api.mimaapp.cn/v8/goods/attribute?trade_relation_id="+type_id+"&cate_id="+gameId+"&goods_cate_id="+gameId;

        String response = HttpRequest.get(url)
                .header("Authorization",authorization)
                .header("Cache-Control","no-cache")
                .header("Connection","keep-alive")
                .header("Content-Type","application/json;charset=UTF-8")
                .header("Device","1")
                .header("Origin","https://h5.mimaapp.cn")
                .header("Pragma","no-cache")
                .header("Referer","https://h5.mimaapp.cn/")
                .header("Sec-Fetch-Dest","empty")
                .header("Sec-Fetch-Mode","cors")
                .header("Sec-Fetch-Site","same-site")
                .header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36")
                .header("cs","")
                .header("et","opnYeMLBhpfAvh/zEbCy/FnIhTrhcpLAKu+I6Mm8jE7zzbd1z1ZR2wMC1lwa42zdcW1mLRaq5NFI5j3nZbkPmg==")
                .header("fp","AtuAHBmeOp5ABhBQQRaHDgzGG2ye5jLy|v1|ca4ddb39add7cf36fa6edede8db40555")
                .header("sec-ch-ua","\"Google Chrome\";v=\"131\", \"Chromium\";v=\"131\", \"Not_A Brand\";v=\"24\"")
                .header("sec-ch-ua-mobile","?0")
                .header("sec-ch-ua-platform","\"Windows\"")
                .execute().body();
        System.out.println(response);

        List<InterAttrRes> interAttrResList = new ArrayList<>();
        Integer status = JSONObject.parseObject(response).getInteger("code");
        if (0 == status) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            String string = JSONObject.parseObject(jsonObject.getString("data")).getString("attrs");
            List<JSONObject> data = JSONObject.parseArray(string, JSONObject.class);
            List<JSONObject> collect = new ArrayList<>();
            if (Objects.equals("1",type)){
                collect = data.stream().filter(d -> d.getString("attribute_component").equals("select-one") || d.getString("attribute_component").equals("select_one_two_column")).collect(Collectors.toList());
            }else if  (Objects.equals("2",type)){
                collect = data.stream().filter(d -> d.getString("attribute_component").equals("select-mult") || d.getString("attribute_component").equals("select_mult_two_column")).collect(Collectors.toList());
            }else if (Objects.equals("3",type)){
                collect = data.stream().filter(d -> d.getString("attribute_component").equals("input")).collect(Collectors.toList());
            }
            for (JSONObject object : collect) {
                InterAttrRes interAttrRes = new InterAttrRes();
                interAttrRes.setUuid(UUIDGenerator.generateUuid6());
                String attribute_id = object.getString("attribute_id");
                String attribute_name = object.getString("attribute_name");
                interAttrRes.setId(attribute_id);
                interAttrRes.setName(attribute_name);
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();

                String attribute_map = object.getString("attribute_map");
                List<JSONObject> list = JSONObject.parseArray(attribute_map, JSONObject.class);
                if(!CollectionUtils.isEmpty(list)) {
                    for (JSONObject s : list) {
                        InterAttrRes interAttrRes1 = new InterAttrRes();
                        interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                        interAttrRes1.setId(s.getString("k"));
                        interAttrRes1.setName(s.getString("v"));
                        interAttrResList1.add(interAttrRes1);
                    }
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }
}